--[[ 打击者1945脚本,显示边缘参考线和火力
  可作为彩京等stg游戏的模版
  
  改装到MyMame模块和MySTG模块
  
  By setycyas @2025-02-11
]]

--防止重复运行
if (_G.game) then
  print('script already loaded!')
  return
end

local myMame = require('MyMame')
local myStg = require('MySTG')

-- 这里设定基本参数
local GAME = {
  -- 脚本名,防重加载用
  NAME = "s1945_autoboot",
  -- 分辨率
  Y_RESOLUTION = 224.0,
  X_RESOLUTION = 320.0,
  -- 横坐标只需要低位,纵坐标需要高低位,无法用读取双字节的方法直接读数据
  Y_ADDR_LOW = 0xFE1119,
  X_ADDR_LOW = 0xFE111D,
  X_ADDR_HIGH = 0xFE111C,
  -- 火力地址
  POW_ADDR = 0xFE1185,
  -- 中心显示方块的宽和高,单位为像素,但WIDTH对应的是X
  BOX_WIDTH = 12,
  BOX_HEIGHT = 12,
  -- 中心方块的边框颜色和中心颜色
  BOX_BORDER_COLOR = 0x880000ff,
  BOX_COLOR = 0x550000ff,
  -- 参考线大小和颜色
  LINE_WIDTH = 1.2,
  LINE_COLOR = 0xFFff0000,
  -- 像素平移
  X_OFFSET = 2,
  Y_OFFSET = 1,
}
-- 更新一下模块的参数
myMame.gameName = GAME.NAME
myMame.screenIndex = ':screen'
myStg.screenIndex = ':screen'
myStg.X_RESOLUTION = GAME.X_RESOLUTION
myStg.Y_RESOLUTION = GAME.Y_RESOLUTION

-- 中心坐标
function GAME.getY()
  local ylow
  ylow = myMame.mem:read_u8(GAME.Y_ADDR_LOW)
  return ylow+GAME.Y_OFFSET
end
function GAME.getX()
  local xlow, xhigh
  xlow = myMame.mem:read_u8(GAME.X_ADDR_LOW)
  xhigh = myMame.mem:read_u8(GAME.X_ADDR_HIGH)
  return xlow+256*xhigh+GAME.X_OFFSET
end

-- 显示监视的数据
function GAME.check()
  local power
  power = myMame.mem:read_u8(GAME.POW_ADDR)
  myMame.container:draw_text(0, 0, "P="..power, 0xFF005500, 0xFFcccccc)
end

-- 根据中心位置显示边缘参考线
function GAME.sideLines(x, y)

  if (x > 0) and (y > 0) then
    myStg.xLine(x-GAME.BOX_WIDTH/2.0, GAME.LINE_COLOR, GAME.LINE_WIDTH)
    myStg.yLine(y-GAME.BOX_HEIGHT/2.0, GAME.LINE_COLOR, GAME.LINE_WIDTH)
    myStg.xLine(x+GAME.BOX_WIDTH/2.0, GAME.LINE_COLOR, GAME.LINE_WIDTH)
    myStg.yLine(y+GAME.BOX_HEIGHT/2.0, GAME.LINE_COLOR, GAME.LINE_WIDTH)
  end
  
end

-- 显示中心
function GAME.showSqua(x, y)

  if (x > 0) and (y > 0) then
    myStg.centerBox(
      x, y,
      GAME.BOX_WIDTH, GAME.BOX_HEIGHT,
      GAME.BOX_BORDER_COLOR, GAME.BOX_COLOR
    )
  end
  
end

-- 显示辅助线
function GAME.showAssist()
  myMame.ui_container:draw_box(0.495, 1.0, 0.505, 0.97, 0xDDdd00dd, 0xDDdd00dd)
end


-- 主函数,绑定后每一帧执行一次该函数
function GAME.Main()
  local x, y

  x = GAME.getX()
  y = GAME.getY()
  
  -- 辅助线
  GAME.showAssist()
  -- 数据
  GAME.check()
  -- 边缘参考线
  GAME.sideLines(x, y)
  -- 显示角色中心方块
  --GAME.showSqua(x, y)

end

--[[运行]]

-- 绑定每一帧执行的函数,测试时绑定到debug
--myMame.debugRunPerFrame(GAME.Main, 's1945')
myMame.runPerFrame(GAME.Main, 's1945')
-- 导出到全局变量
_G.game = GAME

return GAME
